package com.clss.webrtclibrary;


import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.os.Build;
import android.util.Log;
import android.view.Surface;

import com.lgh.uvccamera.UVCCameraProxy;
import com.lgh.uvccamera.callback.ConnectCallback;
import com.serenegiant.usb.Size;

import org.webrtc.CapturerObserver;
import org.webrtc.SurfaceTextureHelper;
import org.webrtc.VideoCapturer;
import org.webrtc.VideoFrame;
import org.webrtc.VideoSink;

import java.util.List;

class UVCCameraCapturerAndroid implements VideoCapturer, VideoSink {
    static final String TAG = "UVCCameraCapturer";

    private int mWidth;
    private int mHeight;
    private SurfaceTextureHelper mSurfaceTextureHelper;
    private CapturerObserver mCapturerObserver;
    private UVCCameraProxy mUVCCameraProxy;
    private Context mContext;
    private Surface mSurface;

    @Override
    public void initialize(SurfaceTextureHelper surfaceTextureHelper, Context applicationContext, CapturerObserver capturerObserver) {
        mCapturerObserver = capturerObserver;
        mSurfaceTextureHelper = surfaceTextureHelper;
        mContext = applicationContext;
        initUVCCamera();
    }

    private void initUVCCamera(){
        Log.i(TAG, "---initUVCCamera===");
        mUVCCameraProxy = new UVCCameraProxy(mContext);
        mUVCCameraProxy.registerReceiver();
        mUVCCameraProxy.getConfig()
                .isDebug(true)
//                .setPicturePath(PicturePath.APPCACHE)
                .setDirName("uvccamera")
                .setProductId(0)
                .setVendorId(0);
        mSurface = new Surface(mSurfaceTextureHelper.getSurfaceTexture());
        mUVCCameraProxy.setPreviewDisplay(mSurface);

    }

    @Override
    public void startCapture(int width, int height, int framerate) {
        Log.i(TAG, "---startCapture===");
        mWidth = 640;
        mHeight = 480;
        mSurfaceTextureHelper.setTextureSize(mWidth, mHeight);
        mUVCCameraProxy.setConnectCallback(new ConnectCallback() {
            @Override
            public void onAttached(UsbDevice usbDevice) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Log.i(TAG, "---onAttached=getDeviceName==" + usbDevice.getDeviceName());
                    Log.i(TAG, "---onAttached=getProductId==" + usbDevice.getProductId());
                    Log.i(TAG, "---onAttached=getVendorId==" + usbDevice.getVendorId());
                    Log.i(TAG, "---onAttached=getDeviceId==" + usbDevice.getDeviceId());
                    Log.i(TAG, "---onAttached=getManufacturerName==" + usbDevice.getManufacturerName());
                    Log.i(TAG, "---onAttached=getProductName==" + usbDevice.getProductName());
                    Log.i(TAG, "---onAttached=getDeviceClass==" + usbDevice.getDeviceClass());
                    Log.i(TAG, "---onAttached=getDeviceSubclass==" + usbDevice.getDeviceSubclass());
                }
                mUVCCameraProxy.requestPermission(usbDevice);
            }

            @Override
            public void onGranted(UsbDevice usbDevice, boolean granted) {
                Log.i(TAG, "---onGranted===" + granted);
                if (granted) {
                    mUVCCameraProxy.connectDevice(usbDevice);
                }
            }

            @Override
            public void onConnected(UsbDevice usbDevice) {
                Log.i(TAG, "---onConnected===");
                mUVCCameraProxy.openCamera();

                List<Size> sizeList = mUVCCameraProxy.getSupportedPreviewSizes();
                Log.i(TAG, "---sizeList===" + sizeList.size());
                for (Size size : sizeList){
                    Lg.i(TAG, "---size==" + size.height + "=" + size.width);
                }
            }

            @Override
            public void onCameraOpened() {
                Log.i(TAG, "---onCameraOpened===");
                mUVCCameraProxy.setPreviewSize(mWidth, mHeight);
                mUVCCameraProxy.startPreview();
            }

            @Override
            public void onDetached(UsbDevice usbDevice) {
                Log.i(TAG, "---onDetached===");
                mUVCCameraProxy.closeCamera();
            }
        });

        mUVCCameraProxy.checkDevice();
        mSurfaceTextureHelper.startListening(this);
    }

    @Override
    public void stopCapture() throws InterruptedException {
        Log.i(TAG, "---stopCapture===");
        mSurfaceTextureHelper.stopListening();
        mCapturerObserver.onCapturerStopped();
        mUVCCameraProxy.stopPreview();
        mUVCCameraProxy.closeCamera();
        mUVCCameraProxy.closeDevice();
        mUVCCameraProxy.unregisterReceiver();
    }

    @Override
    public void changeCaptureFormat(int width, int height, int framerate) {

    }

    @Override
    public void dispose() {

    }

    @Override
    public boolean isScreencast() {
        return false;
    }


    @Override
    public void onFrame(VideoFrame frame) {
        mCapturerObserver.onFrameCaptured(frame);
    }
}
